/**
 * 
 */
package com.googolmo.NowPlaying.SharePreferences;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.googolmo.NowPlaying.Constant;
import com.googolmo.NowPlaying.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/**
 * @author googolmo
 * 
 */
public class MusicPreferences {

	// 写入播放器设置和读取

	/**
	 * 读取播放状态
	 * 
	 * @param contex
	 * @return
	 */
	public static boolean getIsPlaying(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		return sp.getBoolean(Constant.PREF_ISPLAYING, false);
	}

	/**
	 * 写入播放器状态
	 * 
	 * @param context
	 * @param status
	 */
	public static void setIsPlaying(Context context, boolean status) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean(Constant.PREF_ISPLAYING, status);
		editor.commit();
	}

	/**
	 * 获得最近播放的信息
	 * 
	 * @param context
	 * @return
	 */
	public static String getLastPlayInfo(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		return sp.getString(Constant.PREF_LAST_PLAY_INFO, "");
	}

	/**
	 * 设置最近播放的信息
	 * 
	 * @param context
	 * @param info
	 */
	public static void setLastPlayInfo(Context context, String info) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(Constant.PREF_LAST_PLAY_INFO, info);
		editor.commit();
	}

	/**
	 * 获得最后播放的时间
	 * 
	 * @param context
	 * @return
	 */
	public static Date getLastPlayTime(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		String date = sp.getString(Constant.PREF_LAST_PLAY_TIME, "");
		try {
			SimpleDateFormat format = new SimpleDateFormat(Constant.DATEFORMAT);
			return format.parse(date);
		} catch (Exception e) {
			return new Date(System.currentTimeMillis());
		}
	}

	/**
	 * 设置播放时间
	 * 
	 * @param context
	 * @param date
	 */
	public static void setLastPlayTime(Context context, Date date) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		SimpleDateFormat format = new SimpleDateFormat(Constant.DATEFORMAT);
		editor.putString(Constant.PREF_LAST_PLAY_TIME, format.format(date));
		editor.commit();
	}

	/**
	 * 获得耳机状态
	 * 
	 * @param context
	 * @return 0为未插入 1为插入
	 */
	public static int getHandsetPlug(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		return sp.getInt(Constant.PREF_HEADSET_PLUG, 0);
	}

	/**
	 * 写入耳机状态
	 * 
	 * @param context
	 * @param status
	 */
	public static void setHandsetPlug(Context context, int status) {
		SharedPreferences sp = context.getSharedPreferences(Constant.APPNAME,
				Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putInt(Constant.PREF_HEADSET_PLUG, status);
		editor.commit();
	}

	/**
	 * 设置是否开始
	 * 
	 * @param context
	 * @param enable
	 */
	public static void setIsEnabled(Context context, boolean enable) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = sp.edit();
		editor.putBoolean(context.getString(R.string.pre_key_status_on), enable);
		editor.commit();
	}

	/**
	 * 获得是否开启
	 * 
	 * @param context
	 * @return
	 */
	public static boolean getIsEnabled(Context context) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sp.getBoolean(context.getString(R.string.pre_key_status_on),
				true);
	}

	public static boolean getIsToast(Context context) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sp.getBoolean(context.getString(R.string.pre_key_toast), true);
	}

}
